package com.home766.controller.admin;

import com.home766.entity.TeHuiManager;
import com.home766.service.TeHuiManagerService;
import net.biz.Setting;
import net.biz.controller.admin.BaseController;
import net.biz.util.SettingUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Controller
@RequestMapping("/admin/teHui")
public class TeHuiController extends BaseController {
	@Resource(name="teHuiManagerServiceImpl")
	private TeHuiManagerService teHuiManagerService;
	
	@RequestMapping("/pageReturn")
	public String pageReturn(ModelMap model){
		List<TeHuiManager> teHuiManager = teHuiManagerService.findAll();
		model.addAttribute("teHuiManager",teHuiManager);
		return "/admin/tehui/content";
	}
	@RequestMapping("/imageShow")
	public String imageShow(@RequestParam("uploadFile")CommonsMultipartFile[] file, TeHuiManager teHuiManager, HttpServletRequest request, HttpServletResponse response) throws IOException {
		TeHuiManager temp = teHuiManagerService.find(teHuiManager.getId());
		String path = request.getSession().getServletContext().getRealPath("/upload/image");
		Setting setting = SettingUtils.get();
		for(int i=0;i<file.length;i++){
			if(!file[i].isEmpty()){
				//2.输出流,
				FileOutputStream fileOutputStream = new FileOutputStream(path+"/"+file[i].getOriginalFilename());
				//3.输入流
				InputStream inputStream = file[i].getInputStream();
				//读写
				int b =0;
				while((b=inputStream.read())!=-1){
					fileOutputStream.write(b);
				}
				fileOutputStream.flush();
				fileOutputStream.close();
				inputStream.close();
				if(file[0].isEmpty()&&file[1].isEmpty()){
					teHuiManager.setPath(temp.getPath());
					teHuiManager.setBrandPath(temp.getBrandPath());
				}else if(file[0].isEmpty()&&!file[1].isEmpty()){
					teHuiManager.setPath(temp.getPath());
					teHuiManager.setBrandPath(setting.getUploadUrl()+"/"+file[1].getOriginalFilename());
				}else if(!file[0].isEmpty()&&file[1].isEmpty()){
					teHuiManager.setPath(setting.getUploadUrl()+"/"+file[0].getOriginalFilename());
					teHuiManager.setBrandPath(temp.getBrandPath());
				}else if(!file[0].isEmpty()&&!file[1].isEmpty()){
					teHuiManager.setPath(setting.getUploadUrl()+"/"+file[0].getOriginalFilename());
					teHuiManager.setBrandPath(setting.getUploadUrl()+"/"+file[1].getOriginalFilename());
				}
				if(temp!=null){
					teHuiManagerService.update(teHuiManager);
				}else{
					teHuiManagerService.save(teHuiManager);
				}
			}
		}
		return "redirect:pageReturn.jhtml";
	}
}
